//引入数据库
const db = require('../db/index')

//展示该相册下的所有相片
exports.showUserPics = (req, res) => {
    const userinfo = req.query

    const selectSql = 'select * from picture where pic_album = ?'

    db.query(selectSql, userinfo.album_id, (err, results) => {
        if (err) return res.cc(err)

        // console.log(results.length);
        for (let i = 0; i < results.length; i++) {
            results[i].pic_url = results[i].pic_url.split(',')
        }

        const insertSql = 'UPDATE album set album_num=? where album_id=?'
        db.query(insertSql, [results.length, userinfo.album_id], () => {
            return;
        })

        res.send({
            status: 0,
            message: "查询到该相册下所有相片信息",
            data: results
        })
    })
}

exports.uploadPic = (req, res) => {

    const picinfo = req.body

    const selectSql = 'insert into picture set ?'
    db.query(selectSql, { pic_url: (picinfo.pic_url).toString(), pic_des: picinfo.pic_des, pic_album: picinfo.album_id, pic_like: "5" }, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc("数据保存失败!")

        res.send({
            status: 0,
            message: "已经保存所有相片信息",
        })
    })

}